#include "brainstorm.h"
#include "ui_brainstorm.h"
#include <iostream>

BrainStorm::BrainStorm(communicate*com,QJsonObject &json,QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::BrainStorm)
{
    ui->setupUi(this);
    this->com=com;
    connect(com,SIGNAL(receiveSingleQ(QJsonArray )),this,SLOT(receiveSingleQ(QJsonArray )));
    connect(&singleTimer,SIGNAL(timeout()),this,SLOT(singleTimerOut()));
    connect(&rankTimer,SIGNAL(timeout()),this,SLOT(rankTimerOut()));
    connect(com,SIGNAL(Rank(QJsonObject)),this,SLOT(Rank(QJsonObject)));
    userName=json["userName"].toString();
    userRank=json["userRank"].toString();

}

BrainStorm::~BrainStorm()
{
    delete ui;
}
//获取问题
void BrainStorm::singleGetQ()
{
    QJsonObject json;
    json["cmd"]=SINGLE_GETQ;
    this->com->writeClientData(json);

}
//个人题目设置到界面
void BrainStorm::setSingleQ()
{
    ui->singleQLabel->setText(singleQ[num].toObject()["question"].toString());
    ui->singleSelectButton1->setText(singleQ[num].toObject()["selection1"].toString());
    ui->singleSelectButton2->setText(singleQ[num].toObject()["selection2"].toString());
    ui->singleSelectButton3->setText(singleQ[num].toObject()["selection3"].toString());
    ui->singleSelectButton4->setText(singleQ[num].toObject()["selection4"].toString());
}

void BrainStorm::setEnemyScore()
{
    QString str=QString("%1(%2): %3").arg(enemyName,-5).arg(enemyRank).arg(enemyScore);
    ui->enemyscore->setText(str);

}

void BrainStorm::setMyScore()
{
    QString str=QString("%1(%2): %3").arg(userName,-5).arg(userRank).arg(myScore);
    ui->myscore->setText(str);
}

void BrainStorm::singleAnswerQ(int i)
{
    std::cout<<singleQ[num].toObject()["answer"].toInt()<<std::endl;
    if(singleQ[num].toObject()["answer"].toString().toInt()==i)
    {
        score+=20;
        num++;
        time=15;
        if(num==5)
        {
            ui->score->setText("回答结束！");
            QString scorenum=QString("本次得分：%1").arg(this->score);
            ui->scorenum->setText(scorenum);
            ui->stackedWidget->setCurrentWidget(ui->singale);
        }
        else
        {
            setSingleQ();
            ui->lcdNumber->display(time);
            ui->stackedWidget->setCurrentWidget(ui->single_run);
            //开启定时器
            singleTimer.start(1000);
        }

    }
    else
    {
        ui->score->setText("回答错误！");
        QString scorenum=QString("本次得分：%1").arg(this->score);
        ui->scorenum->setText(scorenum);
        ui->stackedWidget->setCurrentWidget(ui->singale);

    }
}

void BrainStorm::rankAnswerQ(int i)
{
    if(RankQ[num2].toObject()["answer"].toString().toInt()==i)
    {
        myScore+=20;
    }
    setMyScore();
    num2++;
    //判断是否下一题
    if(num2==num3)
    {
        rankSec=10;
        rankTimer.stop();
        ui->lcdNumber_2->display(rankSec);
        rankTimer.start(1000);
        setRankQ();
    }
    QJsonObject json;
    json["cmd"]=ANSWER;
    json["enemyName"]=enemyName;
    json["questionId"]=num2;
    json["score"]=myScore;
    com->writeClientData(json);


}

void BrainStorm::setRankQ()
{
    ui->label_4->setText(RankQ[num2].toObject()["question"].toString());
    ui->rankpushButton_1->setText(RankQ[num2].toObject()["selection1"].toString());
    ui->rankpushButton_2->setText(RankQ[num2].toObject()["selection2"].toString());
    ui->rankpushButton_3->setText(RankQ[num2].toObject()["selection3"].toString());
    ui->rankpushButton_4->setText(RankQ[num2].toObject()["selection4"].toString());
    ui->rankpushButton_1->setEnabled(true);
    ui->rankpushButton_2->setEnabled(true);
    ui->rankpushButton_3->setEnabled(true);
    ui->rankpushButton_4->setEnabled(true);
    ui->rankpushButton_1->setStyleSheet("");
    ui->rankpushButton_2->setStyleSheet("");
    ui->rankpushButton_3->setStyleSheet("");
    ui->rankpushButton_4->setStyleSheet("");
    if(num2==5)
    {
        //将结果发送给服务器
        //rankTimer.stop();
        QJsonObject json;
        json["cmd"]=RANKRESULT;
        json["score"]=myScore;
        json["enemyscore"]=enemyScore;
        json["enemyName"]=enemyName;
        com->writeClientData(json);
    }
}

void BrainStorm::setRankResult(QJsonObject json)
{

    QString newRank=json["newrank"].toString();
    if(myScore==enemyScore)
        ui->rankresult->setText("平局");
    else if(myScore<enemyScore)
        ui->rankresult->setText("失败");
    else if(myScore>enemyScore)
        ui->rankresult->setText("胜利");
    QString str=QString("%1---->%2").arg(userRank).arg(newRank);
    ui->newrank->setText(str);
    userRank=newRank;
    ui->stackedWidget->setCurrentWidget(ui->rank_result);
}
//就收数据
void BrainStorm::receiveSingleQ(QJsonArray json)
{
    singleQ=json;
    time=15;
    num=0;
    score=0;
    setSingleQ();
    ui->lcdNumber->display(time);
    ui->stackedWidget->setCurrentWidget(ui->single_run);
    //开启定时器
    singleTimer.start(1000);


}

void BrainStorm::on_pushButton_4_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->singleMenu);
}


void BrainStorm::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->mainMenu);
}

//开始答题
void BrainStorm::on_pushButton_clicked()
{
    this->singleGetQ();
}
//个人计时器
void BrainStorm::singleTimerOut()
{
    --time;
    if(time==0)
    {
        ui->score->setText("回答错误！");
        QString scorenum=QString("本次得分：%1").arg(this->score);
        ui->scorenum->setText(scorenum);
        ui->stackedWidget->setCurrentWidget(ui->singale);
    }
    ui->lcdNumber->display(time);
}

void BrainStorm::rankTimerOut()
{
    --rankSec;
    if(rankSec==0)
    {
        if(ui->rankpushButton_1->isEnabled())
        {
            num2++;
        }
        setRankQ();
        rankSec=10;
    }
    ui->lcdNumber_2->display(rankSec);
}


void BrainStorm::on_pushButton_3_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->mainMenu);
}


void BrainStorm::on_singleSelectButton1_clicked()
{
    singleAnswerQ(1);
}


void BrainStorm::on_singleSelectButton2_clicked()
{
    singleAnswerQ(2);
}


void BrainStorm::on_singleSelectButton3_clicked()
{
    singleAnswerQ(3);
}


void BrainStorm::on_singleSelectButton4_clicked()
{
    singleAnswerQ(4);
}


void BrainStorm::on_rankButton_clicked()
{
    QJsonObject json;
    json["cmd"]=RANK;
    this->com->writeClientData(json);
    //跳到等待界面
    ui->stackedWidget->setCurrentWidget(ui->rank_wait);
}
//排位实现
void BrainStorm::Rank(QJsonObject json)
{
    int cmd=json["cmd"].toInt();
    if(cmd==RANK)
    {
        RankQ=json["question"].toArray();
        enemyName=json["enemyName"].toString();
        enemyRank=json["enemyRank"].toString();
        enemyScore=json["enemyScore"].toInt();
        std::cout<<enemyRank.toStdString().c_str()<<enemyScore<<std::endl;
        rankSec=10;
        myScore=0;
        num2=0;
        num3=0;
        ui->lcdNumber_2->display(rankSec);
        setMyScore();
        setEnemyScore();
        setRankQ();
        rankTimer.start(1000);
        ui->stackedWidget->setCurrentWidget(ui->rank_run);

    }
    else if(cmd==ANSWER)
    {

        enemyScore=json["enemyscore"].toInt();
        std::cout<<enemyScore<<std::endl;
        num3=json["questionId"].toInt();
        setEnemyScore();
        if(num2==num3)
        {
            rankSec=10;
            rankTimer.stop();
            ui->lcdNumber_2->display(rankSec);
            rankTimer.start(1000);
            setMyScore();
            setEnemyScore();
            setRankQ();
        }


    }
    else if(cmd==RANKRESULT)
    {
        std::cout<<json["newrank"].toString().toStdString()<<std::endl;
        setRankResult(json);
    }


}


void BrainStorm::on_rankpushButton_1_clicked()
{
    ui->rankpushButton_1->setStyleSheet("background-color: rgb(85, 170, 255)");
    ui->rankpushButton_1->setEnabled(false);
    ui->rankpushButton_2->setEnabled(false);
    ui->rankpushButton_3->setEnabled(false);
    ui->rankpushButton_4->setEnabled(false);
    rankAnswerQ(1);
}


void BrainStorm::on_rankpushButton_2_clicked()
{
    ui->rankpushButton_2->setStyleSheet("background-color: rgb(85, 170, 255)");
    ui->rankpushButton_1->setEnabled(false);
    ui->rankpushButton_2->setEnabled(false);
    ui->rankpushButton_3->setEnabled(false);
    ui->rankpushButton_4->setEnabled(false);
    rankAnswerQ(2);
}


void BrainStorm::on_rankpushButton_3_clicked()
{
    ui->rankpushButton_3->setStyleSheet("background-color: rgb(85, 170, 255)");
    ui->rankpushButton_1->setEnabled(false);
    ui->rankpushButton_2->setEnabled(false);
    ui->rankpushButton_3->setEnabled(false);
    ui->rankpushButton_4->setEnabled(false);
    rankAnswerQ(3);
}


void BrainStorm::on_rankpushButton_4_clicked()
{
    ui->rankpushButton_4->setStyleSheet("background-color: rgb(85, 170, 255)");
    ui->rankpushButton_1->setEnabled(false);
    ui->rankpushButton_2->setEnabled(false);
    ui->rankpushButton_3->setEnabled(false);
    ui->rankpushButton_4->setEnabled(false);
    rankAnswerQ(4);
}


void BrainStorm::on_rank_result_back_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->mainMenu);
}

